import Vue from 'vue'
import Vuex from 'vuex'
import {gethousinglist,getdetelhousinglist,houslist,getadmin} from '../api/index'
import {Message} from 'element-ui'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo:{},
    routerlist:[],
    hosinglist:[],
    page:{
      pagesize:10,
      pageindex:0,
    },
    length:0,
    redact:{} 
  },
  mutations: {
    // 用户信息
    GET_USER_INFO(state,payload){
     
      state.userinfo=payload.data
    },
    // 路由表信息
    GET_ROUTER_LIST(state,payload){
      state.routerlist=payload
    },
    // 房源列表
    GET_HOSING_LIST(state,payload){
      state.hosinglist=payload
     
    },
    // 分页
    GET_PAGE_LIST(state,payload){
      state.length=payload
      console.log(payload)
    },
    GET_PAGE_INDEX(state,payload){
      state.page.pageindex=payload
    },
    // 编辑
    GET_HOUSE_LIST(state,payload){
       state.redact=payload
      
    },
     // 修改
     GET_AMEND_LIST(state,payload){
       console.log(payload)
     }
  },
  actions: {
      // 房源列表
      async SET_HOSING_LIST(context,page=context.state.page){
        const resp=await gethousinglist(page)
      
        context.commit('GET_HOSING_LIST',resp.data.list)
        context.commit('GET_PAGE_LIST',resp.data.lenth)
      },
      // 删除房源
        SET_DETELHOSING_LIST({dispatch},id){
         getdetelhousinglist(id).then(resp=>{
          
          Message({
            message:resp.data.mess,
            type: 'success'
          })
           dispatch('SET_HOSING_LIST')
         })
            
      },
      // // 编辑
      SET_HOUSE_LIST(context,id){
      houslist(id).then(resp=>{
        // console.log(id)
        context.commit('GET_HOUSE_LIST',resp.data.data)
        context.dispatch('SET_HOSING_LIST')
        // context.commit('SET_HOSING_LIST',resp.data.data)
      })
      },
      // 添加
      SET_ADD_LIST(context,id){
        // console.log(id)
        getadmin(id).then(resp=>{
          context.commit('GET_HOUSE_LIST',resp.data.data)
          context.dispatch('SET_HOSING_LIST')
        })
      }    
  },
  modules: {
  }
})
